package com.twu.biblioteca.book;

import com.twu.biblioteca.item.Field;
import com.twu.biblioteca.item.Item;
import com.twu.biblioteca.message.Message;
import com.twu.biblioteca.utils.StringUtils;

/**
 * Created by Enrico on 10/09/2014.
 */
public class Book extends Item {

    private static final String BOOK_FORMAT = "|%1$-" + Field.NAME.getSize() +
            "s|%2$-" + Field.AUTHOR.getSize() +
            "s|%3$-" + Field.YEAR.getSize() +
            "s|";

    public Book(String name, String author, int year) {
        super(name, author, year);
    }

    @Override
    public String formatted() {
        return String.format(BOOK_FORMAT, " " + name, " " + author, " " + year);
    }

    @Override
    public String toString() {
        return "Book";
    }

    public void showCheckoutMessage(){
        //if item is available then checkout is successful
        String message = (available)
                ? Message.SUCCESSFUL_BOOK_CHECKOUT.getText()
                : Message.UNSUCCESSFUL_BOOK_CHECKOUT.getText();
        System.out.println(message);
    }

    public void showReturnMessage(){
        //if item is not available then return is successful
        String message = (!available)
                ? Message.SUCCESSFUL_BOOK_RETURN.getText()
                : Message.UNSUCCESSFUL_BOOK_RETURN.getText();
        System.out.println(message);
    }

    @Override
    public void showHeader(){
        String header = String.format(BOOK_FORMAT,
                StringUtils.center("Title", Field.NAME.getSize()),
                StringUtils.center("Author(s)", Field.AUTHOR.getSize()),
                StringUtils.center("Year", Field.YEAR.getSize()));
        System.out.println(header);
    }
}
